package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.SharedTemplatePagerAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.TemplateDetailsBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 共享家，样板间信息展示
 *
 * @author fengzhen
 * @version v1.0, 2018/4/20
 */
public class TemplateDetailsActivity extends BasePersonActivity {

    @BindView(R.id.tab_activity_template_details)
    TabLayout mTabActivityTemplateDetails;
    @BindView(R.id.viewpager_activity_template_details)
    ViewPager mViewpagerActivityTemplateDetails;

    @BindArray(R.array.TemplateTitle)
    String[] mTemplateTitles;

    private int mTemplateId;
    private LoadingPagerHead mLoadingPagerHead;
    private TemplateDetailsBean mTemplateDetails;

    /**
     * 跳转样板间详情
     *
     * @param templateId 样板间Id
     */
    public static Intent call2TempalteDetails(Context context, int templateId) {
        Intent intent = new Intent(context, TemplateDetailsActivity.class);
        intent.putExtra(Contans.INTENT_DATA, templateId);
        return intent;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {
        super.initData();

        mTvTitle.setText("样板间详情");
        mTvSmalTitle.setVisibility(View.GONE);
        mTvSmalTitle.setText("");
        mTvSmalTitle.setCompoundDrawablesWithIntrinsicBounds(
                ContextCompat.getDrawable(this, R.mipmap.icon_shared_model_share), null, null, null
        );

        mTemplateId = getIntent().getIntExtra(Contans.INTENT_DATA, -1);

        int color = ContextCompat.getColor(this, R.color.c_29CE7E);
        mTabActivityTemplateDetails.setTabTextColors(ContextCompat.getColor(this, R.color.c_383838), color);
        mTabActivityTemplateDetails.setSelectedTabIndicatorColor(color);

        getData();

    }

    @Override
    protected View getContentView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(this, 0, Contans.SHARHOUSE_SOLDOUT_TYPE) {
            @Override
            protected void close() {
                onBackPressed();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(TemplateDetailsActivity.this, R.layout.activity_template_details);
            }

            @Override
            protected void reLoading() {
                getData();
            }
        };
        return mLoadingPagerHead;
    }

    @Override
    protected void save(View v) {
        super.save(v);
        // TODO: 2018/4/24 分享
        ToastUtils.makeText("分享样板间");
    }

    private void getData() {
        mLoadingPagerHead.showPagerView(Contans.STATE_LOADING);
        Observable<StatusCode<TemplateDetailsBean>> observable = ApiUtils.getApi().getTemplateDetails(Contans.defaultToken, mTemplateId, BaseApplication.getUserId());
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<TemplateDetailsBean>(this) {
                    @Override
                    protected void _onNext(StatusCode<TemplateDetailsBean> statusCode) {
                        mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                        mTemplateDetails = statusCode.getData();

                        SharedTemplatePagerAdapter pagerAdapter = new SharedTemplatePagerAdapter(getSupportFragmentManager(), mTemplateTitles, mTemplateDetails);
                        mViewpagerActivityTemplateDetails.setAdapter(pagerAdapter);
                        mTabActivityTemplateDetails.setupWithViewPager(mViewpagerActivityTemplateDetails, true);
                        mViewpagerActivityTemplateDetails.setOffscreenPageLimit(4);
                    }

                    @Override
                    protected void _onError(String message) {
                        if (Contans.SHARE_HOUSE_SOLD_OUT.equalsIgnoreCase(message)) {
                            mLoadingPagerHead.showPagerView(Contans.STATE_SOLDOUT);
                        } else {
                            mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                            ToastUtils.makeText(message);
                        }
                    }
                }, "", lifecycleSubject, false, true);
    }

    @OnClick({R.id.tv_merchant_layout_template_shared_house, R.id.tv_order_layout_template_shared_house,
            R.id.btn_use_layout_template_shared_house})
    public void onClickM(View view) {
        switch (view.getId()) {

            /**
             * 电话商家
             */
            case R.id.tv_merchant_layout_template_shared_house:
                if (mTemplateDetails.getPhone() != null) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mTemplateDetails.getPhone()));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } else {
                    UIUtils.showCarToast(this, 1, "暂无商家电话!");
                }
                break;

            /**
             * 预约看房
             */
            case R.id.tv_order_layout_template_shared_house:
                if (BaseApplication.isLoginSuccess) {
                    AppointmentsLookHouseActivity.startActivity(TemplateDetailsActivity.this, (long) (mTemplateDetails.getId()), mTemplateDetails.getHouseId(),mTemplateDetails.getFullname(),mTemplateDetails.getHouses());
                } else {
                    LoginActivity.startActivity(TemplateDetailsActivity.this, true);
                }
                break;

            /**
             * 应用到我家
             */
            case R.id.btn_use_layout_template_shared_house:
                if (BaseApplication.isLoginSuccess) {
                    UseMyHomeActivity.startActivity(TemplateDetailsActivity.this, mTemplateDetails.getId(), mTemplateDetails.getId());
                } else {
                    LoginActivity.startActivity(TemplateDetailsActivity.this, true);
                }
                break;
        }
    }
}
